package com.tyqx.hashchainbackend.service.impl;

import com.tyqx.hashchainbackend.service.TrackUserService;
import com.tyqx.hashchainbean.module.TrackUser;
import com.tyqx.hashchainbean.vo.SysUpdaUserPwdVO;
import com.tyqx.hashchaindao.mapper.TrackUserMapper;

import com.tyqx.hashchainutil.common.CreateWallet;
import hashchainutil.common.BackendPage;
import hashchainutil.common.Constants;
import hashchainutil.common.DigestUtil;
import hashchainutil.common.EmptyUtils;
import hashchainutil.exception.HashchainException;
import com.tyqx.hashchainutil.org.app.config.Config;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("trackUserService")
public class TrackUserServiceImpl implements TrackUserService {

    @Resource
    private TrackUserMapper trackUserMapper;


    public TrackUser getTrackUserByUserName(String userName) {
        return trackUserMapper.selectTrackUser(userName);
    }


    public Integer register(TrackUser trackUser)throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        trackUser.setCreateTime(createTime);
        trackUser.setUpdateTime(createTime);
        trackUser.setLoginTime(createTime);
        Integer count=trackUserMapper.add(trackUser);
        CreateWallet createWallet=new CreateWallet();
        createWallet.registerUser(trackUser.getUserName(),Config.ORG1);
        return count;
    }


    public TrackUser select(String userName) {
        return trackUserMapper.selectTrackUser(userName);
    }


    public List<TrackUser> getUidBySkey(String skey) {
        return null ;
    }


    public List<TrackUser> getIdByCompName(String compName) {
        return null;
    }


    public List<TrackUser> getCompNameById(String uid) {
        return null;
    }


    public Integer updateCompName(String uid, String name, String phone, String businessScope) {
        return null;
    }

    @Override
    public TrackUser getById(Long userId) throws Exception {
        return trackUserMapper.getById(userId);
    }

    @Override
    public TrackUser getByUserCode(String userCode) throws Exception {
        return trackUserMapper.getTrackUserByUserName(userCode);
    }

    @Override
    public List<TrackUser> getListByMap(Map<String, Object> param) throws Exception {
        return trackUserMapper.getListByMap(param);
    }

    @Override
    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return trackUserMapper.getCountByMap(param);
    }

    @Override
    public Integer save(TrackUser sysUserSaveVO) throws HashchainException {

        sysUserSaveVO.setCreateTime(new Date());
        sysUserSaveVO.setPassword(DigestUtil.hmacSign(sysUserSaveVO.getPassword()));
        return trackUserMapper.save(sysUserSaveVO);
    }

   /* @Override
    public Integer modify(SysUserModifyVO sysUserModifyVO) throws HashchainException {
        sysUserModifyVO.setUpdateTime(new Date());
        return trackUserMapper.modify(sysUserModifyVO);
    }*/

    @Override
    public Integer removeById(Long userId) throws Exception {
        return trackUserMapper.removeById(userId);
    }

    @Override
    public BackendPage<List<TrackUser>> queryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = trackUserMapper.getCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        param.put("beginPos", (pageNo - 1) * pageSize);
        param.put("pageSize", pageSize);
        List<TrackUser> sysUserList = trackUserMapper.getTrackUserListByMap(param);
        BackendPage page = new BackendPage(total, sysUserList);
        return page;
    }

    @Override
    public Integer updateActivate(Integer userActivate, Long userId) {
        return trackUserMapper.updateActivate(userActivate,userId);
    }

    @Override
    public Integer getUserModify(TrackUser trackUser) throws Exception {
        Subject currentUser = SecurityUtils.getSubject();
        TrackUser currUser=(TrackUser) currentUser.getSession().getAttribute("trackUser");
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        trackUser.setUpdateTime(formatter.parse(dateString));
        return trackUserMapper.getUserModify(trackUser);
    }

    @Override
    public Integer updateUserPwdById(SysUpdaUserPwdVO sysUpdaUserPwdVO) {
        String password = DigestUtil.hmacSign(sysUpdaUserPwdVO.getUserPassword());
        sysUpdaUserPwdVO.setUserPassword(password);
        return trackUserMapper.updateUserPwdById(sysUpdaUserPwdVO);
    }

    @Override
    public Integer delActivate(Integer userAcivate, Long userId) {
        return trackUserMapper.delActivate(userAcivate,userId);
    }
}
